package com.yxy.flybird;

import java.io.IOException;

import org.andengine.audio.music.Music;
import org.andengine.audio.music.MusicFactory;
import org.andengine.audio.sound.Sound;
import org.andengine.audio.sound.SoundFactory;
import org.andengine.opengl.font.Font;
import org.andengine.opengl.font.StrokeFont;
import org.andengine.opengl.texture.ITexture;
import org.andengine.opengl.texture.TextureOptions;
import org.andengine.opengl.texture.atlas.bitmap.BitmapTextureAtlas;
import org.andengine.opengl.texture.atlas.bitmap.BitmapTextureAtlasTextureRegionFactory;
import org.andengine.opengl.texture.region.ITextureRegion;
import org.andengine.opengl.texture.region.TextureRegion;
import org.andengine.ui.activity.SimpleBaseGameActivity;
import org.andengine.util.debug.Debug;

import android.graphics.Color;
import android.graphics.Typeface;

public class ResourceManager {

	// fonts
	Font mScoreFont_best;
	Font mScoreFont;
	Font mGetReadyFont;
	Font mCopyFont;
	StrokeFont mYouSuckFont;

	// sounds
	Sound mScoreSound;
	Sound mDieSound;
	Music mMusic;

	// textures
	BitmapTextureAtlas mBackgroundBitmapTextureAtlas;
	ITextureRegion mBackgroundTextureRegion;
	TextureRegion mInstructionsTexture;

	// jmc bt
	TextureRegion simpleRegion, generalRegion, difficultRegion,
			metamorphosisRegion;

	private SimpleBaseGameActivity context;

	public ResourceManager(SimpleBaseGameActivity context) {
		this.context = context;
	}

	public void createResources() {
		SoundFactory.setAssetBasePath("sound/");
		MusicFactory.setAssetBasePath("sound/");
		BitmapTextureAtlasTextureRegionFactory.setAssetBasePath("img/");

		// background
		mBackgroundBitmapTextureAtlas = new BitmapTextureAtlas(
				context.getTextureManager(), 718, 1184,
				TextureOptions.NEAREST_PREMULTIPLYALPHA);
		mBackgroundTextureRegion = BitmapTextureAtlasTextureRegionFactory
				.createFromAsset(mBackgroundBitmapTextureAtlas,
						context.getAssets(), "background480.png", 0, 0);

		mBackgroundBitmapTextureAtlas.load();

		// instructions img
		BitmapTextureAtlas instructionsTextureAtlas = new BitmapTextureAtlas(
				context.getTextureManager(), 285, 245, TextureOptions.BILINEAR);
		mInstructionsTexture = BitmapTextureAtlasTextureRegionFactory
				.createFromAsset(instructionsTextureAtlas, context,
						"instructions.png", 0, 0);
		instructionsTextureAtlas.load();

		// jmc bt
		BitmapTextureAtlas bta_simple = new BitmapTextureAtlas(
				context.getTextureManager(), 156, 64, TextureOptions.BILINEAR);
		simpleRegion = BitmapTextureAtlasTextureRegionFactory
				.createFromAsset(bta_simple, context, "bs_simple.png", 0, 0);
		bta_simple.load();
		
		BitmapTextureAtlas bta_gene = new BitmapTextureAtlas(
				context.getTextureManager(), 156, 64, TextureOptions.BILINEAR);
		generalRegion = BitmapTextureAtlasTextureRegionFactory
				.createFromAsset(bta_gene, context, "bs_general.png", 0, 0);
		bta_gene.load();
		
		BitmapTextureAtlas bta_dif = new BitmapTextureAtlas(
				context.getTextureManager(), 156, 64, TextureOptions.BILINEAR);
		difficultRegion = BitmapTextureAtlasTextureRegionFactory
				.createFromAsset(bta_dif, context, "bs_difficult.png", 0, 0);
		bta_dif.load();
		
		BitmapTextureAtlas bta_meta = new BitmapTextureAtlas(
				context.getTextureManager(), 320, 160, TextureOptions.BILINEAR);
		metamorphosisRegion = BitmapTextureAtlasTextureRegionFactory
				.createFromAsset(bta_meta, context, "bs_metamorphosis.png", 0, 0);
		bta_meta.load();

		
		PipePair.onCreateResources(context); // let it sort its own resources
												// out
		Bird.onCreateResources(context);
		//Cloud jmc
		Cloud.onCreateResources(context);

		Typeface typeFace = Typeface.createFromAsset(context.getAssets(),
				"flappy.ttf");

		// score board
		final ITexture scoreFontTexture_best = new BitmapTextureAtlas(
				context.getTextureManager(), 256, 256, TextureOptions.BILINEAR);
		mScoreFont_best = new StrokeFont(context.getFontManager(), scoreFontTexture_best,
				typeFace, 48, true, Color.WHITE, 2, Color.BLACK);
		mScoreFont_best.load();
		
		final ITexture scoreFontTexture = new BitmapTextureAtlas(
				context.getTextureManager(), 256, 256, TextureOptions.BILINEAR);
		mScoreFont = new StrokeFont(context.getFontManager(), scoreFontTexture,
				typeFace, 56, true, Color.WHITE, 2, Color.BLACK);
		mScoreFont.load();

		// get ready text
		final ITexture getReadyFontTexture = new BitmapTextureAtlas(
				context.getTextureManager(), 256, 256, TextureOptions.BILINEAR);
		mGetReadyFont = new StrokeFont(context.getFontManager(),
				getReadyFontTexture, typeFace, 60, true, Color.WHITE, 2,
				Color.BLACK);
		mGetReadyFont.load();

		// (c) text
		final ITexture copyFontTexture = new BitmapTextureAtlas(
				context.getTextureManager(), 256, 256, TextureOptions.BILINEAR);
		mCopyFont = new StrokeFont(context.getFontManager(), copyFontTexture,
				typeFace, 32, true, Color.WHITE, 2, Color.BLACK);
		mCopyFont.load();

		// (c) you suck text
		final ITexture youSuckTexture = new BitmapTextureAtlas(
				context.getTextureManager(), 256, 256, TextureOptions.BILINEAR);
		mYouSuckFont = new StrokeFont(context.getFontManager(), youSuckTexture,
				typeFace, 80, true, Color.WHITE, 2, Color.BLACK);
		mYouSuckFont.load();

		// sounds
		try {
			mScoreSound = SoundFactory.createSoundFromAsset(
					context.getSoundManager(), context, "score.ogg");
			mDieSound = SoundFactory.createSoundFromAsset(
					context.getSoundManager(), context, "gameover.ogg");
		} catch (final IOException e) {
			Debug.e(e);
		}

		// music

		try {
			mMusic = MusicFactory.createMusicFromAsset(
					context.getMusicManager(), context, "song.ogg");
			mMusic.setVolume(0.1f);
			mMusic.setLooping(true);
		} catch (final IOException e) {
			Debug.e("Error", e);
		}
	}
}
